package com.mcol.bry.database.hibernate;


import java.util.logging.Logger;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Connection {
	static public void main(String args[])
	{
		new Connection().testDB();
	}
    public void testDB() {
        final Session session = initHibernate();
        try {
            Transaction tx = session.beginTransaction();
            try {
                create(session);
                list(session);

                tx.commit();
                tx = null;
            }
            finally {
                if (tx != null) {
                    tx.rollback();
                    tx = null;
                }
            }
        }
        finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    private void create(final Session session) {
//        Nerd n = new Nerd();
//        n.setName("Ajay");
//        n.setLevel(10);
//        session.save(n);
//
//        n = new Nerd();
//        n.setName("Arkadiy");
//        n.setLevel(100000);
//        session.save(n);
//
//        n = new Nerd();
//        n.setName("Ping");
//        n.setLevel(1);
//        session.save(n);
    }

    private void list(final Session session) {
//        Query q = session.createQuery("from " + Nerd.class.getName());
//        List list = q.list();
//        LOG.info("Query came back with " + list.size() + " results");
//        for (Object row : list) {
//            LOG.debug(row.toString());
//        }
    }

    public Session initHibernate() {
        final Configuration configuration = new Configuration(). 
        		configure("hibernate.cfg.xml");
        System.out.println("Connecting hibernate to URL=" + configuration.getProperty("hibernate.connection.url")
                 + " as user=" + configuration.getProperty("hibernate.connection.username"));
        return configuration.buildSessionFactory().getCurrentSession();
    }
}